<template>
  <div class="navbar">
    <div class="left-side">
      <a-space>
        <img style="width: 36px" src="@/assets/long.png" alt="加载失败" />
        <a-typography-title
          :style="{ margin: 0, fontSize: '18px' }"
          :heading="5"
        >
          CodeInsight AI
        </a-typography-title>
        <icon-menu-fold
          v-if="!topMenu && appStore.device === 'mobile'"
          style="font-size: 22px; cursor: pointer"
          @click="toggleDrawerMenu"
        />
      </a-space>
    </div>
    <div class="center-side">
      <Menu v-if="topMenu" />
    </div>
    <ul class="right-side">
      <li>
        <a-button type="primary" class="publish-btn" @click="handleCreatePost">
          <template #icon>
            <icon-plus />
          </template>
          发布
        </a-button>
      </li>
      <li>
        <a-tooltip content="重新加载">
          <div class="nav-btn" @click="handleReload">
            <icon-sync />
          </div>
        </a-tooltip>
      </li>
      <li>
        <a-tooltip content="应用配置">
          <div class="nav-btn" @click="setVisible">
            <icon-settings />
          </div>
        </a-tooltip>
      </li>
      <li>
        <a-dropdown trigger="click">
          <a-avatar style="margin-right: 8px; cursor: pointer" :size="32">
            <img :src="userInfo.avatar" />
          </a-avatar>
          <template #content>
            <a-doption>
              <a-space @click="switchRoles">
                <icon-tag />
                <span>切换权限</span>
              </a-space>
            </a-doption>
            <a-doption>
              <a-space
                @click="
                  $router.push({
                    name: 'UserInfo',
                    query: { id: userInfo.id }
                  })
                "
              >
                <icon-user />
                <span>用户信息</span>
              </a-space>
            </a-doption>
            <a-doption>
              <a-space @click="$router.push({ name: 'UserSetting' })">
                <icon-settings />
                <span>用户设置</span>
              </a-space>
            </a-doption>
            <a-doption>
              <a-space @click="handleLogout">
                <icon-export />
                <span>退出登录</span>
              </a-space>
            </a-doption>
          </template>
        </a-dropdown>
      </li>
    </ul>
  </div>
  <a-modal :visible="searchModalVisible" @cancel="handleCancelSearchModal">
    <template #title>搜索帖子</template>
    <a-space direction="vertical" fill>
      <a-input-search
        v-model="postTitle"
        size="large"
        placeholder="请输入标题以搜索"
        @search="handleSearch"
        @keydown.enter="handleSearch"
      />
      <search-post-list
        ref="searchRef"
        @close-modal="handleCloseModalByChild"
      />
    </a-space>
    <template #footer>
      <a-button @click="handleCancelSearchModal">取消</a-button>
    </template>
  </a-modal>
</template>

<script lang="ts" setup>
import { computed, inject, ref, unref } from 'vue';
import { Message } from '@arco-design/web-vue';
import { useFullscreen } from '@vueuse/core';
import { useAppStore, useUserStore } from '@/store';
import useUser from '@/hooks/useUser';
import Menu from '@/components/menu/index.vue';
import { useRouter } from 'vue-router';
import SearchPostList from '@/components/navbar/components/search-post-list.vue';

const userInfo = useUserStore();
const appStore = useAppStore();
const userStore = useUserStore();
const { logout } = useUser();
const topMenu = computed(() => appStore.topMenu && appStore.menu);
const setVisible = () => {
  appStore.updateSettings({ globalSettings: true });
};
const handleReload = () => {
  window.location.reload();
};
const handleLogout = () => {
  logout();
};
const switchRoles = async () => {
  const res = await userStore.switchRoles();
  Message.success(res as string);
};
const toggleDrawerMenu = inject('toggleDrawerMenu') as () => void;
const handleCreatePost = () => {
  router.push({ name: 'CreatePost' });
};
const router = useRouter();

const searchModalVisible = ref(false);
const postTitle = ref('');
const searchRef = ref<any>();
// 执行搜索
const handleSearch = () => {
  searchRef.value.searchPost(unref(postTitle));
};
// 点击帖子，关闭搜索框
const handleCloseModalByChild = (flag: boolean) => {
  if (flag) {
    searchModalVisible.value = false;
  }
};
const handleCancelSearchModal = () => {
  searchModalVisible.value = false;
};
</script>

<style lang="less" scoped>
.navbar {
  display: flex;
  justify-content: space-between;
  height: 100%;
  background-color: var(--color-bg-2);
  border-bottom: 1px solid var(--color-border);
}

.left-side {
  display: flex;
  align-items: center;
  padding-left: 20px;
}

.center-side {
  flex: 1;
}

.right-side {
  display: flex;
  padding-right: 5px;
  list-style: none;
  align-items: center;
  margin: 0;

  li {
    display: flex;
    align-items: center;
    padding: 0 5px;
  }

  .search-wrapper {
    position: relative;
    display: flex;
    align-items: center;

    .search-icon {
      position: absolute;
      left: 20px;
      font-size: 16px;
      color: rgb(var(--gray-6));
    }

    .search-input {
      padding-left: 40px;
      border-radius: 20px;
      background-color: var(--color-fill-2);

      :deep(.arco-input-inner-wrapper) {
        background-color: transparent;
        border: none;

        &:hover,
        &:focus {
          background-color: var(--color-fill-3);
        }

        input {
          background-color: transparent;
        }
      }
    }
  }

  .publish-btn {
    display: flex;
    align-items: center;
    height: 32px;
    border-radius: 16px;
    padding: 0 10px;
    background-color: #2759cd;
    border-color: #2759cd;

    &:hover {
      background-color: #1a3a8f;
      border-color: #1a3a8f;
    }

    .send-icon {
      transform: rotate(-45deg);
      display: inline-block;
      margin-right: 6px;
    }
  }

  .nav-btn {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    color: var(--color-text-2);
    border-radius: 50%;
    cursor: pointer;

    &:hover {
      background-color: var(--color-fill-3);
    }
  }
}
</style>

<style lang="less">
.message-popover {
  .arco-popover-content {
    margin-top: 0;
  }
}
</style>
